/*
 * Created on Jan 31, 2008
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.networkInterface;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.*;

public class Ethernet extends GenericNetworkInterface {
	
	private String localIPAddress=null;
	
	public Ethernet()throws Exception{
		this(0);
	}
	public Ethernet(int interfaceNumber)throws Exception{
		Vector<GenericNetworkInterface> interfaces=GenericNetworkInterface.getInterfaces();//getMacAddress();
		System.out.println("IEEE80211: interfaces "+interfaces);
		if(interfaces.size()==0)throw new Exception("There are no network interfaces");

		for(int i=0;i<interfaces.size()/*&&wirelessCount<=wirelessInterfaceNumber*/;i++){
			if(!isEthernet(interfaces.elementAt(i))){
				//System.out.println(interfaces.elementAt(i)+" is NOT wireless ");
				interfaces.removeElementAt(i);
				i--;
			}
		}

		if (interfaceNumber >= interfaces.size())
		{
			throw new Exception("Ethernet "+interfaceNumber+" not found");
		}

		super.windowsNumber=interfaces.elementAt(interfaceNumber).windowsNumber;
		super.hardwareAddress=interfaces.elementAt(interfaceNumber).hardwareAddress;
		super.name=interfaces.elementAt(interfaceNumber).name;
		super.description=interfaces.elementAt(interfaceNumber).description;
	}
	

	public final static boolean isEthernet(GenericNetworkInterface genericNetworkInterface)throws Exception{
		//System.out.println("isEthernet "+genericNetworkInterface);
		boolean res;
		if(System.getProperty("os.name").startsWith("Linux")){
			res=isEthernetLinux(genericNetworkInterface);
		}
		/*else if(System.getProperty("os.name").startsWith("Windows")){	
			res=isEthernetWindows(genericNetworkInterface);
		}*/
		else{
			throw new Exception("Not supported Operating System");
		}
		return res;
	}
	/*private final static boolean isEthernetWindows(GenericNetworkInterface gni){
		return false;
	}*/
	public final static boolean isEthernetLinux(GenericNetworkInterface gni)throws Exception{
		//System.out.println("isEthernetLinux "+gni);
		if(IEEE80211.isIEEE80211(gni)) return false;
		
		//String command="iwconfig eth"+genericNetworkInterface.getNumber();
		String command="ifconfig "+gni.getDescription();
		//System.out.println("isEthernetLinux command: "+command);
		//Process p = Runtime.getRuntime().exec("sudo iwconfig");
		Process p = Runtime.getRuntime().exec(command);
		try{
			p.waitFor();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
		 
		StringBuffer buffer= new StringBuffer();
		int c = stdoutStream.read();
		while(c!=-1){
			buffer.append((char)c);
			c = stdoutStream.read();
		}
		String outputText = buffer.toString();
		//System.out.println("isEthernetLinux outputText\n"+outputText);
		if(outputText.contains("Ethernet")){
			return true;
		}
		else{
			return false;
		}
	}
	
	public String getLocalAddress()throws Exception{
		if (localIPAddress==null){
			if(System.getProperty("os.name").startsWith("Linux")){	
				localIPAddress=getLocalAddressLinux();
			}
			/*else if(System.getProperty("os.name").startsWith("Windows")){
				localIPAddress=getLocalAddressWindows();
			}*/
			else{
				throw new Exception("Not supported Operating System");
			}
		}
		return localIPAddress;
	}
	private String getLocalAddressLinux()throws Exception{
		String res=null;
		String command="ifconfig "+this.getDescription();
		//System.out.println("isEthernetLinux command: "+command);
		//Process p = Runtime.getRuntime().exec("sudo iwconfig");
		Process p = Runtime.getRuntime().exec(command);
		try{
			p.waitFor();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
		 
		StringBuffer buffer= new StringBuffer();
		int c = stdoutStream.read();
		while(c!=-1){
			buffer.append((char)c);
			c = stdoutStream.read();
		}
		String outputText = buffer.toString();
		
		String lines[]=outputText.split("\n");
		for(int i=0;i<lines.length&&res==null;i++){
			//System.out.println("Ethernet.getLocalAddressLinux lines["+i+"] "+lines[i]);
			if(lines[i].contains("inet addr")){
				//System.out.println("Ethernet.getLocalAddressLinux found inet addr");
				String tokens[]=lines[i].split("[ :]");
				for(int j=0;j<tokens.length&&res==null;j++){
					//System.out.println("Ethernet.getLocalAddressLinux tokens["+j+"] "+tokens[j]);
					if(tokens[j].equals("addr")){
						//System.out.println("Ethernet.getLocalAddressLinux found localIPAddress "+tokens[j+1]);
						res=tokens[j+1];
					}
				}
			}
		}
		return res;
	}
}
